import java.util.*;

public class Solution {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
    }


    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> ans = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return ans;
        }
        queue.add(root);
        boolean even = false;
        while(!queue.isEmpty()){
            LinkedList<Integer> tmp = new LinkedList<>();
            for(int i = 0; i < queue.size(); i++){
                TreeNode cur = queue.remove();
                if(even == true){
                    tmp.addFirst(cur.val);
                }
                if(cur.left != null) queue.add(cur.left);
                if(cur.right != null) queue.add(cur.right);
            }
            even = even ? false : true;
            ans.add(tmp);
        }
        return ans;
    }

    public int findBottomLeftValue(TreeNode root) {
        TreeNode cur = root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            cur = queue.remove();
            if(cur.right != null) queue.add(cur.right);
            if(cur.left != null) queue.add(cur.left);
        }
        return cur.val;
    }
}
